package com.bootdo.clouddocommon.intercepter;

import com.bootdo.clouddocommon.constants.CommonConstants;
import com.bootdo.clouddocommon.context.FilterContextHandler;
import com.bootdo.clouddocommon.dto.UserToken;
import com.bootdo.clouddocommon.utils.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class AuthIntercepter extends HandlerInterceptorAdapter {
    private static final Logger logger = LoggerFactory.getLogger(HandlerInterceptorAdapter.class);

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String token = request.getHeader(CommonConstants.CONTEXT_TOKEN);
////        UserToken userToken = JwtUtils.getInfoFromToken(token);
//        FilterContextHandler.setToken(token);
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//
//        Class clazz = principal.getClass();
//        String className = clazz.getSimpleName(); // 取出当前角色的数据类型，若为CurrentUser，则提取出其中的所有信息
//        if ("CurrentUser".equals(className)){
//
//            Method getName = clazz.getMethod("getName");
//            Method getId = clazz.getMethod("getId");
//            Method getUsername = clazz.getMethod("getUsername");
//
//            Object name = getName.invoke(principal);
//            Object userName = getUsername.invoke(principal);
//            Object id_L = getId.invoke(principal);
//
//            String id = id_L.toString();
//
//            logger.info("------设置token"+Thread.currentThread().getId());
//
//            FilterContextHandler.setUsername((String) userName);
//            FilterContextHandler.setName((String) name);
//            FilterContextHandler.setUserID(id);
//        }
//        return super.preHandle(request, response, handler);
//    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        获取token
        String token = request.getHeader(CommonConstants.CONTEXT_TOKEN);
        FilterContextHandler.setToken(token);

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentPrincipalName = authentication.getName();

        String userName = request.getHeader(CommonConstants.USER_NAME);
        FilterContextHandler.setUsername(userName);

        String userId = request.getHeader(CommonConstants.USER_ID);
        FilterContextHandler.setUserID(userId);


//        FilterContextHandler.setUsername((String) userName);
//        FilterContextHandler.setName((String) name);
//        FilterContextHandler.setUserID(id);

        return super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        FilterContextHandler.remove();
        super.afterCompletion(request, response, handler, ex);
    }
}
